package com.example.photography.fragment.im;

import android.os.Bundle;
import android.view.View;

import com.example.photography.Activity.ChatActivity;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.manager.EaseSystemMsgManager;
import com.hyphenate.easeui.modules.conversation.EaseConversationListFragment;
import com.hyphenate.easeui.utils.EaseCommonUtils;

import java.util.List;

//会话列表页面
public
class ConversationListFragment extends EaseConversationListFragment {

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        //监听会话消息
        EMClient.getInstance().chatManager().addMessageListener(emMessageLister);

    }

    @Override
    public void finishRefresh() {
        super.finishRefresh();
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        Object item = conversationListLayout.getItem(position).getInfo();
        if(item instanceof EMConversation) {
            if(EaseSystemMsgManager.getInstance().isSystemConversation((EMConversation) item)) {
                //系统信息
            }else {
                ChatActivity.actionStart(getActivity(), ((EMConversation)item).conversationId(), EaseCommonUtils.getChatType((EMConversation) item));
            }
        }

    }
    //通过注册消息监听来接收消息
    private EMMessageListener emMessageLister = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {
            EaseIM.getInstance().getNotifier().notify(messages);
            onRefresh();
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> messages) {

        }

        @Override
        public void onMessageRead(List<EMMessage> messages) {

        }

        @Override
        public void onMessageDelivered(List<EMMessage> messages) {

        }

        @Override
        public void onMessageRecalled(List<EMMessage> messages) {

        }

        @Override
        public void onMessageChanged(EMMessage message, Object change) {

        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        EMClient.getInstance().chatManager().removeMessageListener(emMessageLister);

    }
}
